package br.furb.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import br.furb.controller.PedidoController;
import br.furb.model.ApplicationSession;
import br.furb.model.negocio.ItemMenu;
import br.furb.model.negocio.Usuario;
import br.furb.util.Imagem;
import br.furb.util.Observer;
import br.furb.util.Subject;

public class CadastroPedido extends JDialog implements Subject {

	private final JPanel contentPanel = new JPanel();
	private PedidoController pedidoController;
	private JDialog me;
	private JButton btnCancelar;
	private JButton btnConfirmar;
	private JPanel panelCabecalho;
	private JLabel lblNumeroPedido;
	private JLabel lblNumero;
	private JPanel panelResumoPedido;
	private JPanel panelAtendente;
	private JLabel lblAtendente;
	private JComboBox<Usuario> comboAtendente;
	private JList<ItemMenu> list;
	private DefaultListModel<ItemMenu> listData;
	private JLabel lblValor;
	private JLabel lblValorTotal;
	private JPanel panel;
	private JPanel panel_1;
	private JScrollPane scrollPane;
	private JPanel panelItens;
	private List<Observer> observers = new ArrayList<Observer>();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			CadastroPedido dialog = new CadastroPedido(null);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public CadastroPedido(JFrame owner) {
		super(owner, true);
		setSize(new Dimension(400, 420));
		setTitle("Novo Pedido");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(owner);
		setResizable(false);
		
		me = this;
		pedidoController = new PedidoController(this);
		
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			panelCabecalho = new JPanel();
			contentPanel.add(panelCabecalho, BorderLayout.NORTH);
			panelCabecalho.setLayout(new BorderLayout(0, 0));
			{
				panel = new JPanel();
				panelCabecalho.add(panel, BorderLayout.WEST);
				{
					lblNumeroPedido = new JLabel("N\u00FAmero Pedido:");
					lblNumeroPedido.setFont(new Font("Tahoma", Font.BOLD, 12));
					panel.add(lblNumeroPedido);
				}
				{
					lblNumero = new JLabel();
					panel.add(lblNumero);
				}
			}
			{
				panel_1 = new JPanel();
				panelCabecalho.add(panel_1, BorderLayout.EAST);
				{
					lblValorTotal = new JLabel("Valor Total:");
					lblValorTotal.setPreferredSize(new Dimension(70, 15));
					lblValorTotal.setFont(new Font("Tahoma", Font.BOLD, 12));
					panel_1.add(lblValorTotal);
				}
				{
					lblValor = new JLabel("0");
					lblValor.setPreferredSize(new Dimension(50, 15));
					panel_1.add(lblValor);
				}
			}
		}
		{
			panelResumoPedido = new JPanel();
			contentPanel.add(panelResumoPedido, BorderLayout.WEST);
			{
				listData = new DefaultListModel<ItemMenu>();
				list = new JList<ItemMenu>(listData);
				list.setBackground(UIManager.getColor("Panel.background"));
				list.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
				list.setPreferredSize(new Dimension(150, 250));
				list.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						if (e.getClickCount() == 2){
							listData.remove(list.getSelectedIndex());
						}
					}
				});
				panelResumoPedido.add(list);
			}
		}
		{
			panelAtendente = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
			contentPanel.add(panelAtendente, BorderLayout.SOUTH);
			{
				lblAtendente = new JLabel("Atendente:");
				lblAtendente.setFont(new Font("Tahoma", Font.BOLD, 12));
				panelAtendente.add(lblAtendente);
			}
			{
				comboAtendente = new JComboBox<Usuario>();
				comboAtendente.setPreferredSize(new Dimension(290, 20));
				panelAtendente.add(comboAtendente);
			}
		}
		{
			panelItens = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
//			scrollPane = new JScrollPane();
//			scrollPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
//			scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//			contentPanel.add(scrollPane, BorderLayout.CENTER);
			contentPanel.add(panelItens, BorderLayout.CENTER);
//			{
//				panelItens = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
//				scrollPane.setViewportView(panelItens);
//			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				btnConfirmar = new JButton("Confirmar");
				btnConfirmar.setFont(new Font("Tahoma", Font.BOLD, 12));
				btnConfirmar.setActionCommand("confirmar");
				btnConfirmar.setIcon(Imagem.icone("confirm.png"));
				btnConfirmar.addActionListener(this.pedidoController);
				buttonPane.add(btnConfirmar);
				getRootPane().setDefaultButton(btnConfirmar);
			}
			{
				btnCancelar = new JButton("Cancelar");
				btnCancelar.setFont(new Font("Tahoma", Font.BOLD, 12));
				btnCancelar.setActionCommand("cancelar");
				btnCancelar.setIcon(Imagem.icone("cancel.png"));
				btnCancelar.addActionListener(this.pedidoController);
				buttonPane.add(btnCancelar);
			}
		}
	}

	public void carregaItensMenu() {
		List<ItemMenu> itensMenu = ApplicationSession.getInstance().getCardapio().getItensMenu();
		
		JButton botao;
		for (final ItemMenu itemMenu : itensMenu) {
			botao = new JButton(itemMenu.getDescricao(), itemMenu.getIcone());
			botao.setPreferredSize(new Dimension(100, 30));
			botao.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					listData.addElement(itemMenu);
					lblValor.setText(Double.parseDouble(lblValor.getText()) + itemMenu.getValor() + "");
				}
			});
			panelItens.add(botao);
		}
	}

	@Override
	public void addObserver(Observer o) {
		observers.add(o);
	}

	@Override
	public void removeObserver(Observer o) {
		observers.remove(o);
	}

	@Override
	public void removeObserver(int i) {
		observers.remove(i);
	}

	@Override
	public void notifyObservers() {
		for (Observer o : observers) {
			o.update(pedidoController.getModel());
		}
	}
	
	@Override
	public void dispose() {
		limparCampos();
		super.dispose();
	}
	
	public void setVisible(boolean b, int id) {
		pedidoController.novoId(id);
		lblNumero.setText(Integer.toString(id));
		
		List<Usuario> u = ApplicationSession.getInstance().getUsuarios();
		if (u != null){
			comboAtendente.removeAllItems();
			for (Usuario usuario : u) {
				comboAtendente.addItem(usuario);
			}
		}
		
		carregaItensMenu();
		
		super.setVisible(b);
	}
	
	private void limparCampos() {
		lblValor.setText("");
		listData.clear();
		if (comboAtendente.getItemCount() > 0)
			comboAtendente.setSelectedIndex(0);
	}
	
	public List<ItemMenu> getItens() {
		return Collections.list(listData.elements());
	}
	
	public Usuario getAtendente() {
		return (Usuario) comboAtendente.getSelectedItem();
	}
}